package utilities.file;

import java.io.FileNotFoundException;
import java.io.RandomAccessFile;

import logic.PayrollRecord;

/**
 * 
 * @author Geoffrey Shaw, Kristofer Deviat
 * 
 * The classes offers all of the utilities needed to parse a binary file.
 *
 */
public class BinaryFileIn extends RandomAccessFile {
	
	private static final String MODE = "rw";
	
	public BinaryFileIn(String fileName) throws FileNotFoundException {
		super(fileName, MODE);
	}
	
	/**
	 * Reads a payroll record from the binary file at the selected location.
	 * @param loc location to start reading from.
	 * @return
	 */
	public PayrollRecord readBinaryFile(int loc)
	{
		PayrollRecord c = new PayrollRecord();
		
		try
		{
			seek(((loc * c.SIZE) - c.SIZE) + c.offset_payroll_date);
			c.setPayroll_date(readUTF());
			
			seek(((loc * c.SIZE) - c.SIZE) + c.offset_emp_id);
			c.setEmp_Id(readUTF());
			
			seek(((loc * c.SIZE) - c.SIZE) + c.offset_amount);
			c.setAmount(readDouble());
			
			seek(((loc * c.SIZE) - c.SIZE) + c.offset_Status);
			c.setStatus(readUTF());
		}
		catch(Exception e)
		{
			c = null;
		}
		
		return c;
	}
}
